//
//  DHSearchSceneItemView.m
//  Dresshere
//
//  Created by ZhangPeng on 2018/1/14.
//  Copyright © 2018年 张朋. All rights reserved.
//

#import "DHSearchSceneItemView.h"

@interface DHSearchSceneItemView()

@end

@implementation DHSearchSceneItemView

- (instancetype)initWithFrame:(CGRect)frame andSceneTitle:(NSString *)title
{
    self = [super initWithFrame:frame];
    if (self) {
        if (!self.sceneButton) {
            self.sceneButton = [[UIButton alloc] initWithFrame:CGRectMake(5, 10.5, frame.size.width - 10, 33)];
            [self.sceneButton setTitle:title forState:UIControlStateNormal];
            self.sceneButton.titleLabel.font = [UIFont systemFontOfSize:14];
            [self.sceneButton setTitleColor:[UIColor colorWithHexString:@"#4A494A"] forState:UIControlStateNormal];
            self.sceneButton.layer.cornerRadius = 33/2;
            
            [self.sceneButton addTarget:self action:@selector(clickAction:) forControlEvents:UIControlEventTouchUpInside];
        }
        [self addSubview:self.sceneButton];
    }
    return self;
}

- (void)clickAction:(UIButton *)sender{
    sender.selected = !sender.selected;
    if (sender.isSelected) {
        UIImageView *bgImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, sender.size.width, sender.size.height)];
        bgImageView.tag = 1000;
        bgImageView.image = [UIImage imageNamed:@"button_bg_1px"];
        bgImageView.layer.cornerRadius = 33/2;
        bgImageView.layer.masksToBounds = YES;
        bgImageView.alpha = 0;
        [sender insertSubview:bgImageView atIndex:0];
        [sender setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [UIView animateWithDuration:0.2 animations:^{
            bgImageView.alpha = 1.0;
        }];
    }else{
        for (UIView *view in sender.subviews) {
            if (view.tag == 1000) {
                [view removeFromSuperview];
            }
        }
        [sender setTitleColor:[UIColor colorWithHexString:@"#4A494A"] forState:UIControlStateNormal];
    }
}

@end
